<template>
  <div class="login-container">
    <a-card class="login-card" :bordered="false">
      <a-tabs v-model:activeKey="activeKey">
        <a-tab-pane key="login" tab="登录">
          <a-form
            :model="loginForm"
            :rules="loginRules"
            @finish="handleLogin"
            layout="vertical"
          >
            <a-form-item name="username" label="用户名">
              <a-input v-model:value="loginForm.username" placeholder="请输入用户名">
                <template #prefix>
                  <user-outlined />
                </template>
              </a-input>
            </a-form-item>
            <a-form-item name="password" label="密码">
              <a-input-password v-model:value="loginForm.password" placeholder="请输入密码">
                <template #prefix>
                  <lock-outlined />
                </template>
              </a-input-password>
            </a-form-item>
            <a-form-item>
              <a-button type="primary" html-type="submit" class="login-button" :loading="loading">
                登录
              </a-button>
            </a-form-item>
          </a-form>
        </a-tab-pane>
        
        <a-tab-pane key="register" tab="注册">
          <a-form
            :model="registerForm"
            :rules="registerRules"
            @finish="handleRegister"
            layout="vertical"
          >
            <a-form-item name="username" label="用户名">
              <a-input v-model:value="registerForm.username" placeholder="请输入用户名">
                <template #prefix>
                  <user-outlined />
                </template>
              </a-input>
            </a-form-item>
            <a-form-item name="password" label="密码">
              <a-input-password v-model:value="registerForm.password" placeholder="请输入密码">
                <template #prefix>
                  <lock-outlined />
                </template>
              </a-input-password>
            </a-form-item>
            <a-form-item name="repeatPassword" label="确认密码">
              <a-input-password v-model:value="registerForm.repeatPassword" placeholder="请再次输入密码">
                <template #prefix>
                  <safety-outlined />
                </template>
              </a-input-password>
            </a-form-item>
            <a-form-item name="email" label="邮箱">
              <a-input v-model:value="registerForm.email" placeholder="请输入邮箱">
                <template #prefix>
                  <mail-outlined />
                </template>
              </a-input>
            </a-form-item>
            <a-form-item>
              <a-button type="primary" html-type="submit" class="login-button" :loading="loading">
                注册
              </a-button>
            </a-form-item>
          </a-form>
        </a-tab-pane>
      </a-tabs>
    </a-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import { UserOutlined, LockOutlined, SafetyOutlined, MailOutlined } from '@ant-design/icons-vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { register as registerApi } from '@/api/user'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 活动标签
const activeKey = ref('login')
const loading = ref(false)

// 根据URL参数切换到指定标签
onMounted(() => {
  if (route.query.tab === 'register') {
    activeKey.value = 'register'
  }
})

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: '',
})

// 注册表单数据
const registerForm = reactive({
  username: '',
  password: '',
  repeatPassword: '',
  email: '',
  phone: ''
})

// 登录表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度必须在3-20个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }
  ]
}

// 注册表单验证规则
const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度必须在3-20个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }
  ],
  repeatPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { 
      validator: (rule, value) => {
        if (value === registerForm.password) {
          return Promise.resolve()
        }
        return Promise.reject('两次输入的密码不一致')
      },
      trigger: 'blur'
    }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ]
}

// 处理登录
const handleLogin = async (values) => {
  loading.value = true
  
  try {
    // 调用 store 中的登录方法
    const result = await userStore.login(values)
  } catch (error) {
    console.error('登录失败:', error)
  } finally {
    loading.value = false
  }
}

// 处理注册
const handleRegister = async (values) => {
  loading.value = true
  
  try {
    // 调用注册 API
    await registerApi(values)
    
    message.success('注册成功，请登录')
    // 切换到登录标签
    activeKey.value = 'login'
    // 预填充用户名
    loginForm.username = values.username
  } catch (error) {
    console.error('注册失败:', error)
  } finally {
    loading.value = false
  }
}

// 处理忘记密码
const handleForgotPassword = () => {
  message.info('请联系管理员重置密码')
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: #f0f2f5;
  background-image: url('https://gw.alipayobjects.com/zos/rmsportal/TVYTbAXWheQpRcWDaDMu.svg');
  background-repeat: no-repeat;
  background-position: center 110px;
  background-size: 100%;
}

.login-card {
  width: 400px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.09);
}

.login-button {
  width: 100%;
}

.forgot-link {
  float: right;
  color: #1890ff;
}
</style> 